package com.mstore.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.SessionAware;

import com.mstore.pojo.Account;
import com.mstore.pojo.Broadcast;
import com.mstore.pojo.Shop;
import com.mstore.pojo.User;
import com.mstore.service.BFuncPrivilegeService;
import com.mstore.service.BalanceService;
import com.mstore.service.BaseMemberService;
import com.mstore.service.BroadcastService;
import com.mstore.service.ConsumeService;
import com.mstore.service.ExamineSettingService;
import com.mstore.service.ShopService;

public class BroadcastAction extends BaseAction implements SessionAware {

	private static final long serialVersionUID = -943155788414944429L;

	private BroadcastService broadcastService;

	private List<Broadcast> broadcasts;

	private Broadcast broadcast;

	private Map<String, Object> session;

	private String result;

	private int line;

	private int index;

	private int page;

	private static Logger logger = Logger.getLogger(BroadcastAction.class
			.getName());

	/**
	 * 日期
	 */
	private String dateView;

	/**
	 * 企业账户余额
	 */
	private Float rbalance;

	/**
	 * 今日交易数量
	 */
	private Integer tradeCount;

	/**
	 * 今日收入
	 */
	private Double dateMoney;

	/**
	 * 今日充值
	 */
	private Double dateBalance;

	/**
	 * 新增会员
	 */
	private Integer increaseMember;

	private ConsumeService consumeService;

	private BaseMemberService baseMemberService;

	private ShopService shopService;

	private BFuncPrivilegeService funcPrivilegeService;

	private BalanceService balanceService;

	private ExamineSettingService examineSettingService;

	/**
	 * 新建消息
	 */
	public String addBroadcast() {
		try {
			Object obj = session.get("account");
			if (obj == null) {
				result = "系统异常，请重新登陆";
				return "error";
			}
			if (broadcast == null) {
				result = "消息不能为空";
				return "error";
			}

			if (StringUtils.isBlank(broadcast.getTitle())) {
				result = "消息标题不能为空";
				return "error";
			}

			if (StringUtils.isBlank(broadcast.getContext())) {
				result = "消息内容不能为空";
				return "error";
			}

			Account account = (Account) obj;
			Long eaId = account.getId();

			broadcast.setCreateTime(getCreateTime());
			broadcast.setEaId(eaId);

			int privilege = funcPrivilegeService.getCountFuncPrivilegeByFuncId(
					eaId, 5L);

			if (privilege > 0) {
				// 系统级消息
				broadcast.setLevel(0);
			}
			else {
				broadcast.setLevel(1);
			}

			broadcastService.addBroadcast(broadcast);
		}
		catch (Exception e) {
			logger.error("新建发布消息报错", e);
		}

		return SUCCESS;
	}

	/**
	 * 删除消息
	 */
	public String delBroadcast() {
		if (broadcast != null) {
			if (broadcast.getId() != null) {
				broadcastService.delBroadcast(broadcast);
			}
		}
		return SUCCESS;
	}

	/**
	 * 分页查询企业消息
	 */
	public String getBroadcastByPage() {
		try {
			Object obj = session.get("account");
			if (obj == null) {
				result = "系统异常，请重新登陆";
				return "error";
			}
			Account account = (Account) obj;
			Long eaId = account.getId();

			line = broadcastService.getCountBroadcastByEaId(eaId);

			if (this.line == 0) {
				this.line = 1;
			}
			if (this.line % 14 == 0) {
				this.page = this.line / 14;
			}
			else {
				this.page = (this.line / 14) + 1;
			}

			if (index < 1)
				index = 1;
			else if (index > page)
				index = page;

			broadcasts = broadcastService.getBroadcastByPage(index, eaId);
		}
		catch (Exception e) {
			logger.error("分页查询企业消息报错", e);
		}

		return "getBroadcastByPage";
	}

	/**
	 * 跳转到修改页面
	 */
	public String initModifyBroadcast() {
		if (broadcast == null) {
			result = "系统异常，请重新登陆";
			return "error";
		}

		Long id = broadcast.getId();
		if (id == null) {
			result = "系统异常，请重新登陆";
			return "error";
		}

		broadcast = broadcastService.getBroadcastById(id);
		if (broadcast == null) {
			result = "系统异常，请重新登陆";
			return "error";
		}

		session.remove("broadcast");
		session.put("broadcast", broadcast);

		return "toModify";
	}

	/**
	 * 修改消息
	 */
	public String modifyBroadcast() {
		try {
			if (broadcast == null) {
				result = "消息不能为空";
				return "error";
			}

			if (StringUtils.isBlank(broadcast.getTitle())) {
				result = "消息标题不能为空";
				return "error";
			}

			String context = broadcast.getContext();
			if (StringUtils.isBlank(context)) {
				result = "消息内容不能为空";
				return "error";
			}

			context = context.replaceAll("'", "");

			broadcast.setContext(context);

			broadcastService.modifyBroadcast(broadcast);
		}
		catch (Exception e) {
			logger.error("修改消息报错", e);
		}

		return SUCCESS;
	}

	/**
	 * 预览消息
	 */
	public String previewBroadcast() {
		if (broadcast == null) {
			result = "系统异常，请重新登陆";
			return "error";
		}

		Long id = broadcast.getId();
		if (id == null) {
			result = "系统异常，请重新登陆";
			return "error";
		}

		broadcast = broadcastService.getBroadcastById(id);
		return "preview";
	}

	/**
	 * 查看消息
	 */
	public String lookMiniBroadcast() {
		if (broadcast == null) {
			result = "系统异常，请重新登陆";
			return "error";
		}

		Long id = broadcast.getId();
		if (id == null) {
			result = "系统异常，请重新登陆";
			return "error";
		}

		broadcast = broadcastService.getBroadcastById(id);
		return "lookMini";
	}

	/**
	 * 进入首页
	 */
	public String goHome() {
		try {
			Broadcast broadcast = new Broadcast();
			broadcast.setLevel(0);
			broadcast.setIndex(0);
			broadcast.setLimit(3);
			// 系统消息
			List<Broadcast> broadcasts0 = broadcastService
					.getBroadcastByConditions(broadcast);
			// 企业消息
			Object obj = session.get("account");
			if (obj == null) {
				result = "系统异常，请重新登陆";
				return "error";
			}
			Account account = (Account) obj;
			Long eaId = account.getId();
			broadcast.clear();
			broadcast.setEaId(eaId);
			broadcast.setLevel(1);
			broadcast.setIndex(0);
			broadcast.setLimit(8);
			List<Broadcast> broadcasts1 = broadcastService
					.getBroadcastByConditions(broadcast);

			broadcasts = new ArrayList<Broadcast>();
			for (Broadcast b0 : broadcasts0) {
				broadcasts.add(b0);
			}

			for (Broadcast b1 : broadcasts1) {
				broadcasts.add(b1);
			}

			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd",
					Locale.SIMPLIFIED_CHINESE);
			// 日期
			dateView = sdf.format(date);
			// 企业账户余额
			rbalance = account.getRbalance();

			String begin = dateView + " 00:00:00";
			String end = dateView + " 23:59:59";
			// 今日交易数量
			tradeCount = consumeService.getCountConsumeByTime(eaId, begin, end)
					.intValue();

			// 今日新增会员
			increaseMember = baseMemberService.getCountBaseMemberByTime(eaId,
					begin, end).intValue();
			
			// 今日充值
			dateBalance = balanceService.getSumBalanceByTime(eaId, begin, end);
			
			// 今日收入
			dateMoney = consumeService.getSumConsumeByTime(eaId, begin, end);
			/*
			if (eaId == LogConsumeExamineAction.xiaoheAccountId) {
				// 为杭州晓荷公司定制开发的今日收入
				Consume consume = new Consume();
				consume.setEaId(eaId);
				consume.setExamineFlag(1);
				consume.setBeginTime(begin);
				consume.setEndTime(end);
				dateMoney = examineSettingService.getSumConsumesByCondition(consume);
			}else {
				// 普通商户的今日收入
				dateMoney = consumeService.getSumConsumeByTime(eaId, begin, end);
			}
			*/
		}
		catch (Exception e) {
			logger.error("进入首页报错", e);
		}

		return "toHome";
	}

	/**
	 * 进入分店首页
	 */
	public String goUserHome() {
		try {
			Broadcast broadcast = new Broadcast();
			broadcast.setLevel(0);
			broadcast.setIndex(0);
			broadcast.setLimit(3);
			// 系统消息
			List<Broadcast> broadcasts0 = broadcastService
					.getBroadcastByConditions(broadcast);
			// 企业消息
			Object obj = session.get("user");
			if (obj == null) {
				result = "系统异常，请重新登陆";
				return "error";
			}
			User user = (User) obj;
			Long shopId = user.getShopId();
			Shop shop = new Shop();
			shop.setId(shopId);
			shop = shopService.getShopById(shop);

			if (shop == null) {
				result = "系统异常，请重新登陆";
				return "error";
			}

			Long eaId = shop.getEaId();

			broadcast.clear();
			broadcast.setEaId(eaId);
			broadcast.setLevel(1);
			broadcast.setIndex(0);
			broadcast.setLimit(8);
			List<Broadcast> broadcasts1 = broadcastService
					.getBroadcastByConditions(broadcast);

			broadcasts = new ArrayList<Broadcast>();
			for (Broadcast b0 : broadcasts0) {
				broadcasts.add(b0);
			}

			for (Broadcast b1 : broadcasts1) {
				broadcasts.add(b1);
			}

			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd",
					Locale.SIMPLIFIED_CHINESE);
			// 日期
			dateView = sdf.format(date);
			// 企业账户余额 (分店无法看到企业账户余额)
			rbalance = 0.0f;

			String begin = dateView + " 00:00:00";
			String end = dateView + " 23:59:59";
			// 今日交易数量
			tradeCount = consumeService.getCountConsumeByTimeAndShopId(shopId,
					begin, end).intValue();

			// 今日充值
			dateBalance = balanceService.getSumBalanceByTimeAndShopId(shopId,
					begin, end);

			// 今日新增会员
			increaseMember = baseMemberService.getCountBaseMemberByTime(eaId,
					begin, end).intValue();
			
			// 今日收入
			dateMoney = consumeService.getSumConsumeByTime(eaId, begin, end);
			/*
			if (eaId == LogConsumeExamineAction.xiaoheAccountId) {
				// 为杭州晓荷公司定制开发的今日收入
				Consume consume = new Consume();
				consume.setEaId(eaId);
				consume.setExamineFlag(1);
				consume.setBeginTime(begin);
				consume.setEndTime(end);
				dateMoney = examineSettingService.getSumConsumesByCondition(consume);
			}else {
				// 普通商户的今日收入
				
			}
			*/
		}
		catch (Exception e) {
			logger.error("进入分店首页报错", e);
		}

		return "toUserHome";
	}

	public BroadcastService getBroadcastService() {
		return broadcastService;
	}

	public void setBroadcastService(BroadcastService broadcastService) {
		this.broadcastService = broadcastService;
	}

	public List<Broadcast> getBroadcasts() {
		return broadcasts;
	}

	public void setBroadcasts(List<Broadcast> broadcasts) {
		this.broadcasts = broadcasts;
	}

	public Broadcast getBroadcast() {
		return broadcast;
	}

	public void setBroadcast(Broadcast broadcast) {
		this.broadcast = broadcast;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public int getLine() {
		return line;
	}

	public void setLine(int line) {
		this.line = line;
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public String getDateView() {
		return dateView;
	}

	public void setDateView(String dateView) {
		this.dateView = dateView;
	}

	public Float getRbalance() {
		return rbalance;
	}

	public void setRbalance(Float rbalance) {
		this.rbalance = rbalance;
	}

	public Integer getTradeCount() {
		return tradeCount;
	}

	public void setTradeCount(Integer tradeCount) {
		this.tradeCount = tradeCount;
	}

	public Double getDateMoney() {
		return dateMoney;
	}

	public void setDateMoney(Double dateMoney) {
		this.dateMoney = dateMoney;
	}

	public Integer getIncreaseMember() {
		return increaseMember;
	}

	public void setIncreaseMember(Integer increaseMember) {
		this.increaseMember = increaseMember;
	}

	public ConsumeService getConsumeService() {
		return consumeService;
	}

	public void setConsumeService(ConsumeService consumeService) {
		this.consumeService = consumeService;
	}

	public BaseMemberService getBaseMemberService() {
		return baseMemberService;
	}

	public void setBaseMemberService(BaseMemberService baseMemberService) {
		this.baseMemberService = baseMemberService;
	}

	public ShopService getShopService() {
		return shopService;
	}

	public void setShopService(ShopService shopService) {
		this.shopService = shopService;
	}

	public BFuncPrivilegeService getFuncPrivilegeService() {
		return funcPrivilegeService;
	}

	public void setFuncPrivilegeService(
			BFuncPrivilegeService funcPrivilegeService) {
		this.funcPrivilegeService = funcPrivilegeService;
	}

	public Double getDateBalance() {
		return dateBalance;
	}

	public void setDateBalance(Double dateBalance) {
		this.dateBalance = dateBalance;
	}

	public BalanceService getBalanceService() {
		return balanceService;
	}

	public void setBalanceService(BalanceService balanceService) {
		this.balanceService = balanceService;
	}

	public ExamineSettingService getExamineSettingService() {
		return examineSettingService;
	}

	public void setExamineSettingService(
			ExamineSettingService examineSettingService) {
		this.examineSettingService = examineSettingService;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

}
